package me.zhengjie.pm.projectstage.service.impl;

import me.zhengjie.pm.projectstage.domain.ProjectStage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectstage.repository.ProjectStageRepository;
import me.zhengjie.pm.projectstage.service.ProjectStageService;
import me.zhengjie.pm.projectstage.service.dto.ProjectStageDto;
import me.zhengjie.pm.projectstage.service.dto.ProjectStageQueryCriteria;
import me.zhengjie.pm.projectstage.service.mapper.ProjectStageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectStage")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectStageServiceImpl implements ProjectStageService {

    private final ProjectStageRepository projectStageRepository;

    private final ProjectStageMapper projectStageMapper;

    public ProjectStageServiceImpl(ProjectStageRepository projectStageRepository, ProjectStageMapper projectStageMapper) {
        this.projectStageRepository = projectStageRepository;
        this.projectStageMapper = projectStageMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectStageQueryCriteria criteria, Pageable pageable){
        Page<ProjectStage> page = projectStageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectStageMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectStageDto> queryAll(ProjectStageQueryCriteria criteria){
        return projectStageMapper.toDto(projectStageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectStageDto findById(Integer id) {
        ProjectStage projectStage = projectStageRepository.findById(id).orElseGet(ProjectStage::new);
        ValidationUtil.isNull(projectStage.getId(),"ProjectStage","id",id);
        return projectStageMapper.toDto(projectStage);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectStageDto create(ProjectStage resources) {
        return projectStageMapper.toDto(projectStageRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectStage resources) {
        ProjectStage projectStage = projectStageRepository.findById(resources.getId()).orElseGet(ProjectStage::new);
        ValidationUtil.isNull( projectStage.getId(),"ProjectStage","id",resources.getId());
        projectStage.copy(resources);
        projectStageRepository.save(projectStage);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectStageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectStageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectStageDto projectStage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目ID", projectStage.getProjectBase().getId());
            map.put("项目阶段名称", projectStage.getStageName());
            map.put("项目阶段描述", projectStage.getStageDesc());
            map.put("预期起始时间", projectStage.getPlanStartTime());
            map.put("预期完成时间", projectStage.getPlanFinishTime());
            map.put("实际阶段开始时间", projectStage.getStartTime());
            map.put("实际阶段结束时间", projectStage.getEndTime());
            map.put("阶段在整个项目占比", projectStage.getRatio());
            map.put("阶段工作完成状态", projectStage.getWorkState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}